/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaFinBuscaAssociado.java
 *
 * Created on 24/04/2011, 00:36:47
 */
package asfabdesk.apresentacao;

import asfabdesk.dominio.Associado;
import asfabdesk.dominio.Dependente;
import asfabdesk.presistencia.DAO;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Marco Aurelio
 */
public class TelaFinBuscaAssociado extends javax.swing.JInternalFrame {

    private List<Associado> associados = null;
    private List<Dependente> dependentes = null;
    public static Associado associado;
    public static Dependente dependente;

    /** Creates new form TelaFinBuscaAssociado */
    public TelaFinBuscaAssociado() {
        initComponents();
        associado = null;
        dependente = null;
    }

    public void buscarAssociado() {
        if (txtAssociado.getText().equals("")) {
//            JOptionPane.showMessageDialog(null, "Ops, você não preencheu o campo nome!", "Campos Obrigatórios.", 2);
        } else {
            associados = new DAO().listar(Associado.class, "AssociadosPorNome", "%" + txtAssociado.getText() + "%");
            tabelaAssociados.setModel(new ModeloAssociado(associados));
        }

    }

    public void buscarDependente() {
        if (txtDependente.getText().equals("")) {
//            JOptionPane.showMessageDialog(null, "Ops, você não preencheu o campo nome!", "Campos Obrigatórios.", 2);
        } else {
            dependentes = new DAO().listar(Associado.class, "DependentesPorNome", "%" + txtDependente.getText() + "%");
            tabelaDependentes.setModel(new ModeloDependente(dependentes));
        }

    }

    private class ModeloAssociado extends DefaultTableModel {

        private List<Associado> lista;
        private String[] campos = new String[]{"Nome", "Matrícula", "Ativo"};

        public ModeloAssociado(List<Associado> list) {
            lista = list;
        }

        public ModeloAssociado() {
        }

        public Associado getAssociado(int index) {
            return lista.get(index);
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        @Override
        public Object getValueAt(int row, int column) {
            Associado associado = lista.get(row);


            switch (column) {
                case 0:
                    return associado.getNome();

                case 1:
                    return associado.getMatricula();

                case 2:
                    return associado.isAtivo();

                default:
                    return null;
            }
        }

        @Override
        public int getRowCount() {
            return lista == null ? 0 : lista.size();
        }

        @Override
        public int getColumnCount() {
            return campos.length;
        }

        @Override
        public String getColumnName(int column) {
            return campos[column];
        }

        @Override
        public Class<?> getColumnClass(int column) {
            if (column == 2) {
                return Boolean.class;
            }
            return super.getColumnClass(column);
        }
    }

    private class ModeloDependente extends DefaultTableModel {

        private List<Dependente> lista;
        private String[] campos = new String[]{"Nome", "Ativo"};

        public ModeloDependente(List<Dependente> list) {
            lista = list;
        }

        public ModeloDependente() {
        }

        public Dependente getDependente(int index) {
            return lista.get(index);
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        @Override
        public Object getValueAt(int row, int column) {
            Dependente dependente = lista.get(row);


            switch (column) {
                case 0:
                    return dependente.getNome();

                case 1:
                    return dependente.isAtivo();

                default:
                    return null;
            }
        }

        @Override
        public int getRowCount() {
            return lista == null ? 0 : lista.size();
        }

        @Override
        public int getColumnCount() {
            return campos.length;
        }

        @Override
        public String getColumnName(int column) {
            return campos[column];
        }

        @Override
        public Class<?> getColumnClass(int column) {
            if (column == 1) {
                return Boolean.class;
            }
            return super.getColumnClass(column);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaAssociados = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtAssociado = new javax.swing.JTextField();
        btnOkAssociado = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaDependentes = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        txtDependente = new javax.swing.JTextField();
        btnOkDependente = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        jPanel4.setName("jPanel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPanel5.setName("jPanel5"); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPanel6.setName("jPanel6"); // NOI18N

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setClosable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaFinBuscaAssociado.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel7.setName("jPanel7"); // NOI18N
        jPanel7.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        jTabbedPane1.setFont(resourceMap.getFont("jTabbedPane1.font")); // NOI18N
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jTabbedPane1.setOpaque(true);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabelaAssociados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Matricula", "Ativo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaAssociados.setName("tabelaAssociados"); // NOI18N
        tabelaAssociados.setOpaque(false);
        tabelaAssociados.setSelectionBackground(resourceMap.getColor("tabelaAssociados.selectionBackground")); // NOI18N
        tabelaAssociados.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabelaAssociados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaAssociadosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaAssociados);
        tabelaAssociados.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tabelaAssociados.columnModel.title1")); // NOI18N
        tabelaAssociados.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tabelaAssociados.columnModel.title0")); // NOI18N
        tabelaAssociados.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tabelaAssociados.columnModel.title4")); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtAssociado.setText(resourceMap.getString("txtAssociado.text")); // NOI18N
        txtAssociado.setName("txtAssociado"); // NOI18N
        txtAssociado.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtAssociadoCaretUpdate(evt);
            }
        });

        btnOkAssociado.setText(resourceMap.getString("btnOkAssociado.text")); // NOI18N
        btnOkAssociado.setName("btnOkAssociado"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnOkAssociado, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAssociado, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtAssociado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnOkAssociado)
                .addGap(6, 6, 6))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setOpaque(false);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tabelaDependentes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Ativo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaDependentes.setName("tabelaDependentes"); // NOI18N
        tabelaDependentes.setOpaque(false);
        tabelaDependentes.setSelectionBackground(resourceMap.getColor("tabelaDependentes.selectionBackground")); // NOI18N
        tabelaDependentes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabelaDependentes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaDependentesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tabelaDependentes);
        tabelaDependentes.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tabelaAssociados.columnModel.title1")); // NOI18N
        tabelaDependentes.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tabelaAssociados.columnModel.title4")); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtDependente.setName("txtDependente"); // NOI18N
        txtDependente.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtDependenteCaretUpdate(evt);
            }
        });

        btnOkDependente.setText(resourceMap.getString("btnOkDependente.text")); // NOI18N
        btnOkDependente.setName("btnOkDependente"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDependente, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE))
                    .addComponent(btnOkDependente, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDependente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnOkDependente)
                .addGap(7, 7, 7))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jPanel7.add(jTabbedPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 480, 290));

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel7.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jSeparator1.setBackground(resourceMap.getColor("jSeparator1.background")); // NOI18N
        jSeparator1.setName("jSeparator1"); // NOI18N
        jPanel7.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 480, 10));

        jPanel3.add(jPanel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 500, 360));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaAssociadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaAssociadosMouseClicked
        if (evt.getClickCount() >= 2) {
            try {
                associado = (((ModeloAssociado) tabelaAssociados.getModel()).getAssociado(tabelaAssociados.getSelectedRow()));
                dispose();
            } catch (Exception ex) {
            }
        }
}//GEN-LAST:event_tabelaAssociadosMouseClicked

    private void tabelaDependentesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaDependentesMouseClicked
        if (evt.getClickCount() >= 2) {
            try {
                dependente = (((ModeloDependente) tabelaDependentes.getModel()).getDependente(tabelaDependentes.getSelectedRow()));
                dispose();
            } catch (Exception ex) {
            }
        }
    }//GEN-LAST:event_tabelaDependentesMouseClicked

    private void txtAssociadoCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtAssociadoCaretUpdate
        buscarAssociado();
    }//GEN-LAST:event_txtAssociadoCaretUpdate

    private void txtDependenteCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtDependenteCaretUpdate
        buscarDependente();
    }//GEN-LAST:event_txtDependenteCaretUpdate

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOkAssociado;
    private javax.swing.JButton btnOkDependente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tabelaAssociados;
    private javax.swing.JTable tabelaDependentes;
    private javax.swing.JTextField txtAssociado;
    private javax.swing.JTextField txtDependente;
    // End of variables declaration//GEN-END:variables
}
